/* JPopupMenuDemo.java 
* Created on Nov 29, 2012 
*@Author: Mr.Viet 
*@Phone: 01683430426 
*@Email: phamvanvietcdhd@gmail.com
*/ 
package com.session2; 

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class JPopupMenuDemo extends JFrame implements MouseListener,ActionListener{
	private JTextArea txtNote;
	private JPopupMenu pMenu;
	private JMenuItem miCopy,miPase,miDel;
	public void createForm(){
		this.txtNote = new JTextArea();
		this.pMenu = new JPopupMenu();
		this.miCopy = new JMenuItem("Copy");
		this.miPase = new JMenuItem("Pase");
		this.miDel = new JMenuItem("Delete");
		
		this.pMenu.add(this.miCopy);
		this.pMenu.add(this.miPase);
		this.pMenu.addSeparator();
		this.pMenu.add(this.miDel);
		
		this.getContentPane().add(new JScrollPane(this.txtNote));
		this.txtNote.addMouseListener(this);
		this.miCopy.addActionListener(this);
	}
	public void showForm(){
		this.setTitle("Demo Menu");
		this.setSize(500,500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		JPopupMenuDemo d = new JPopupMenuDemo();
		d.createForm();
		d.showForm();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.miCopy){
			JOptionPane.showMessageDialog(this, "OK ,Copping");
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if(e.isPopupTrigger()){
			this.pMenu.show(e.getComponent(), e.getX(), e.getY());
		}
	}

}
